home *** CD-ROM | disk | FTP | other *** search
Text File | 1995-11-08 | 21.2 KB | 704 lines | [TEXT/MPS ] |
- //========================================================================================
- //
- // File: FWRect.cpp
- // Release Version: $ 1.0d11 $
- //
- // Copyright: © 1993, 1995 by Apple Computer, Inc., all rights reserved.
- //
- //========================================================================================
-
- #include "FWOS.hpp"
-
- #ifndef FWRECT_H
- #include "FWRect.h"
- #endif
-
- // ----- Foundation Includes -----
-
- #ifndef FWFXMATH_H
- #include "FWFxMath.h"
- #endif
-
- #ifndef FWSTREAM_H
- #include "FWStream.h"
- #endif
-
- // ----- OpenDoc Includes -----
-
- #ifndef _ODTYPES_
- #include <ODTypes.h>
- #endif
-
- //========================================================================================
- // RunTime Info
- //========================================================================================
-
- #if FW_LIB_EXPORT_PRAGMAS
- #pragma lib_export on
- #endif
-
- #ifdef FW_BUILD_MAC
- #pragma segment fwodutil
- #endif
-
- //========================================================================================
- // struct FW_SPlatformRect
- //========================================================================================
-
- //----------------------------------------------------------------------------------------
- // FW_SPlatformRect::FW_SPlatformRect
- //----------------------------------------------------------------------------------------
-
- FW_SPlatformRect::FW_SPlatformRect(const FW_CRect& rect)
- {
- left = rect.left.AsInt();
- top = rect.top.AsInt();
- right = rect.right.AsInt();
- bottom = rect.bottom.AsInt();
- }
-
- //----------------------------------------------------------------------------------------
- // FW_SPlatformRect::operator =
- //----------------------------------------------------------------------------------------
-
- FW_SPlatformRect& FW_SPlatformRect::operator =(const FW_CRect& rect)
- {
- left = rect.left.AsInt();
- top = rect.top.AsInt();
- right = rect.right.AsInt();
- bottom = rect.bottom.AsInt();
-
- return *this;
- }
-
- //----------------------------------------------------------------------------------------
- // FW_SPlatformRect::operator==
- //----------------------------------------------------------------------------------------
-
- FW_Boolean FW_SPlatformRect::operator==(const FW_SPlatformRect& rect) const
- {
- return
- left == rect.left &&
- top == rect.top &&
- right == rect.right &&
- bottom == rect.bottom;
- }
-
- //----------------------------------------------------------------------------------------
- // FW_SPlatformRect::operator!=
- //----------------------------------------------------------------------------------------
-
- FW_Boolean FW_SPlatformRect::operator!=(const FW_SPlatformRect& rect) const
- {
- return
- left != rect.left ||
- top != rect.top ||
- right != rect.right ||
- bottom != rect.bottom;
- }
-
- //========================================================================================
- // class FW_CRect
- //========================================================================================
-
- //----------------------------------------------------------------------------------------
- // FW_CRect::FW_CRect
- //----------------------------------------------------------------------------------------
-
- FW_CRect::FW_CRect() :
- left (FW_IntToFixed(0)),
- top (FW_IntToFixed(0)),
- right (FW_IntToFixed(0)),
- bottom (FW_IntToFixed(0))
- {
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CRect::FW_CRect
- //----------------------------------------------------------------------------------------
-
- FW_CRect::FW_CRect(FW_CFixed l, FW_CFixed t, FW_CFixed r, FW_CFixed b) :
- left (l),
- top (t),
- right (r),
- bottom (b)
- {
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CRect::FW_CRect
- //----------------------------------------------------------------------------------------
-
- FW_CRect::FW_CRect(const FW_CPoint &topLeft, const FW_CPoint &botRight)
- {
- left = topLeft.x; right = botRight.x;
- top = topLeft.y; bottom = botRight.y;
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CRect::FW_CRect
- //----------------------------------------------------------------------------------------
-
- FW_CRect::FW_CRect(const FW_CPoint &startPoint, const FW_CPoint &endPoint, FW_CFixed penSize)
- {
- left = startPoint.x;
- right = endPoint.x;
- top = startPoint.y;
- bottom= endPoint.y;
-
- Sort();
-
- right -= penSize;
- bottom -= penSize;
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CRect::FW_CRect
- //----------------------------------------------------------------------------------------
-
- FW_CRect::FW_CRect(const FW_CPoint &topLeft, FW_CFixed width, FW_CFixed height )
- {
- left = topLeft.x; right = left + width;
- top = topLeft.y; bottom= top + height;
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CRect::FW_CRect
- //----------------------------------------------------------------------------------------
-
- FW_CRect::FW_CRect(const ODRect& rect) :
- left (FW_ODFixedToFixed(rect.left)),
- top (FW_ODFixedToFixed(rect.top)),
- right (FW_ODFixedToFixed(rect.right)),
- bottom (FW_ODFixedToFixed(rect.bottom))
- {
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CRect::FW_CPoint
- //----------------------------------------------------------------------------------------
-
- FW_CRect::FW_CRect(const FW_CReadableStream& stream)
- {
- stream.Read(this, sizeof(ODRect));
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CRect:: operator=
- //----------------------------------------------------------------------------------------
-
- FW_CRect& FW_CRect:: operator=(const ODRect& odRect)
- {
- left = FW_ODFixedToFixed(odRect.left); right = FW_ODFixedToFixed(odRect.right);
- top = FW_ODFixedToFixed(odRect.top); bottom= FW_ODFixedToFixed(odRect.bottom);
-
- return *this;
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CRect::FW_CRect
- //----------------------------------------------------------------------------------------
-
- FW_CRect::FW_CRect(const FW_PlatformRect& plfmRect) :
- left (FW_IntToFixed(plfmRect.left)),
- top (FW_IntToFixed(plfmRect.top)),
- right (FW_IntToFixed(plfmRect.right)),
- bottom (FW_IntToFixed(plfmRect.bottom))
- {
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CRect::FW_CRect
- //----------------------------------------------------------------------------------------
-
- FW_CRect::FW_CRect(const FW_CRect& rect) :
- left (rect.left),
- top (rect.top),
- right (rect.right),
- bottom (rect.bottom)
- {
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CRect::operator=
- //----------------------------------------------------------------------------------------
-
- FW_CRect& FW_CRect::operator=(const FW_PlatformRect& plfmRect)
- {
- left = FW_IntToFixed(plfmRect.left); right = FW_IntToFixed(plfmRect.right);
- top = FW_IntToFixed(plfmRect.top); bottom= FW_IntToFixed(plfmRect.bottom);
-
- return *this;
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CRect:: operator=
- //----------------------------------------------------------------------------------------
-
- FW_CRect& FW_CRect:: operator=(const FW_CRect& rect)
- {
- left = rect.left; right = rect.right;
- top = rect.top; bottom= rect.bottom;
-
- return *this;
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CRect::Set
- //----------------------------------------------------------------------------------------
-
- void FW_CRect::Set(FW_CFixed l, FW_CFixed t, FW_CFixed r, FW_CFixed b)
- {
- left = l; right = r;
- top = t; bottom= b;
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CRect::Set
- //----------------------------------------------------------------------------------------
-
- void FW_CRect::Set(const FW_CPoint &topLeft, FW_CFixed width, FW_CFixed height)
- {
- left = topLeft.x; right = left+width;
- top = topLeft.y; bottom= top +height;
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CRect::SetInt
- //----------------------------------------------------------------------------------------
-
- void FW_CRect::SetInt(short l, short t, short r, short b)
- {
- left = FW_IntToFixed(l); right = FW_IntToFixed(r);
- top = FW_IntToFixed(t); bottom= FW_IntToFixed(b);
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CRect::Offset
- //----------------------------------------------------------------------------------------
-
- void FW_CRect::Offset(FW_CFixed x, FW_CFixed y)
- {
- left += x; right += x;
- top += y; bottom+= y;
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CRect::Offset
- //----------------------------------------------------------------------------------------
-
- void FW_CRect::Offset(const FW_CPoint &pt)
- {
- left += pt.x; right += pt.x;
- top += pt.y; bottom+= pt.y;
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CRect::Place
- //----------------------------------------------------------------------------------------
-
- void FW_CRect::Place(FW_CFixed x, FW_CFixed y)
- {
- right += x - left;
- left = x;
- bottom += y - top;
- top = y;
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CRect::Place
- //----------------------------------------------------------------------------------------
-
- void FW_CRect::Place(const FW_CPoint &pt)
- {
- right += pt.x - left;
- left = pt.x;
- bottom += pt.y - top;
- top = pt.y;
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CRect::PlaceInCenter
- //----------------------------------------------------------------------------------------
-
- void FW_CRect::PlaceInCenter(const FW_CRect& otherRect)
- {
- FW_CFixed newTop = (otherRect.bottom + otherRect.top - bottom + top).Half();
- bottom = newTop + bottom - top;
- top = newTop;
-
- FW_CFixed newLeft = (otherRect.right + otherRect.left - right + left).Half();
- right = newLeft + right - left;
- left = newLeft;
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CRect::Inset
- //----------------------------------------------------------------------------------------
-
- void FW_CRect::Inset(FW_CFixed x, FW_CFixed y )
- {
- left += x;
- right -= x;
- top += y;
- bottom -= y;
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CRect::Clear
- //----------------------------------------------------------------------------------------
-
- void FW_CRect::Clear( )
- {
- left = right = top = bottom = FW_IntToFixed(0);
- }
-
- //----------------------------------------------------------------------------------------
- // operator|= FW_CPoint
- //----------------------------------------------------------------------------------------
-
- void FW_CRect:: operator|= (const FW_CPoint &pt)
- {
- left = FW_Minimum(left,pt.x);
- right= FW_Maximum(right,pt.x);
- top = FW_Minimum(top,pt.y);
- bottom=FW_Maximum(bottom,pt.y);
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CRect::AsPlatformRect
- //----------------------------------------------------------------------------------------
-
- void FW_CRect::AsPlatformRect(FW_PlatformRect &plfmRect) const
- {
- plfmRect.left = left.AsInt();
- plfmRect.top = top.AsInt();
- plfmRect.right = right.AsInt();
- plfmRect.bottom = bottom.AsInt();
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CRect::IsEmpty
- //----------------------------------------------------------------------------------------
-
- FW_Boolean FW_CRect::IsEmpty() const
- {
- return right<=left || bottom<=top;
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CRect::Contains
- //----------------------------------------------------------------------------------------
-
- FW_Boolean FW_CRect::Contains(const FW_CPoint &pt) const
- {
- return left<=pt.x && pt.x<right
- && top <=pt.y && pt.y<bottom;
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CRect::Contains
- //----------------------------------------------------------------------------------------
-
- FW_Boolean FW_CRect::Contains( const FW_CRect &r) const
- {
- return left<=r.left && r.right<=right
- && top <=r.top && r.bottom<=bottom;
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CRect::operator==
- //----------------------------------------------------------------------------------------
-
- FW_Boolean FW_CRect::operator==(const FW_CRect &r) const
- {
- return (left == r.left && top == r.top && right == r.right && bottom == r.bottom);
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CRect::IsIntersecting
- //----------------------------------------------------------------------------------------
-
- FW_Boolean FW_CRect::IsIntersecting(const FW_CRect &r) const
- {
- return FW_Maximum(left,r.left) < FW_Minimum(right,r.right)
- && FW_Maximum(top,r.top) < FW_Minimum(bottom,r.bottom);
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CRect::Intersection
- //----------------------------------------------------------------------------------------
-
- void FW_CRect::Intersection(const FW_CRect &r)
- {
- left = FW_Maximum(left, r.left);
- top = FW_Maximum(top, r.top);
- right = FW_Minimum(right, r.right);
- bottom = FW_Minimum(bottom, r.bottom);
-
- if (IsEmpty())
- SetInt(0,0,0,0);
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CRect::Intersection
- //----------------------------------------------------------------------------------------
-
- void FW_CRect::Intersection(const FW_CRect &r1, const FW_CRect &r2)
- {
- left = FW_Maximum(r1.left, r2.left);
- top = FW_Maximum(r1.top, r2.top);
- right = FW_Minimum(r1.right, r2.right);
- bottom = FW_Minimum(r1.bottom, r2.bottom);
-
- if (IsEmpty())
- SetInt(0,0,0,0);
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CRect::Union
- //----------------------------------------------------------------------------------------
-
- void FW_CRect::Union(const FW_CRect &rect)
- {
- left = FW_Minimum(left, rect.left);
- right= FW_Maximum(right, rect.right);
- top = FW_Minimum(top, rect.top);
- bottom=FW_Maximum(bottom, rect.bottom);
-
- if (IsEmpty())
- SetInt(0,0,0,0);
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CRect::Union
- //----------------------------------------------------------------------------------------
-
- void FW_CRect::Union(const FW_CRect &r1, const FW_CRect &r2)
- {
- left = FW_Minimum(r1.left, r2.left);
- right= FW_Maximum(r1.right, r2.right);
- top = FW_Minimum(r1.top, r2.top);
- bottom=FW_Maximum(r1.bottom, r2.bottom);
-
- if (IsEmpty())
- SetInt(0,0,0,0);
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CRect::Sort
- //----------------------------------------------------------------------------------------
-
- void FW_CRect::Sort()
- {
- if (left > right)
- {
- FW_CFixed temp = left;
- left = right;
- right = temp;
- }
- if (top > bottom)
- {
- FW_CFixed temp = top;
- top = bottom;
- bottom = temp;
- }
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CRect::Map
- //----------------------------------------------------------------------------------------
- // The result may not be sorted. You might have to call Sort.
-
- void FW_CRect::Map(const FW_CRect& srcRect, const FW_CRect& dstRect)
- {
- FW_CFixed ratio = (right - left) / (srcRect.right - srcRect.left);
-
- left += ratio * (dstRect.left - srcRect.left);
- right += ratio * (dstRect.right - srcRect.right);
-
-
- ratio = (bottom - top) / (srcRect.bottom - srcRect.top);
-
- top += ratio * (dstRect.top - srcRect.top);
- bottom += ratio * (dstRect.bottom - srcRect.bottom);
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CRect::Transform
- //----------------------------------------------------------------------------------------
-
- void FW_CRect::Transform(Environment* ev, ODTransform* transform)
- {
- #if FW_OPENDOC_VERSION < FW_OPENDOC_DR4
- * (((ODPoint*) this) + 0) =
- #endif
- transform->TransformPoint(ev, ((ODPoint*) this) + 0);
-
- #if FW_OPENDOC_VERSION < FW_OPENDOC_DR4
- * (((ODPoint*) this) + 1) =
- #endif
- transform->TransformPoint(ev, ((ODPoint*) this) + 1);
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CRect::InverseTransform
- //----------------------------------------------------------------------------------------
-
- void FW_CRect::InverseTransform(Environment* ev, ODTransform* transform)
- {
- #if FW_OPENDOC_VERSION < FW_OPENDOC_DR4
- * (((ODPoint*) this) + 0) =
- #endif
- transform->InvertPoint(ev, ((ODPoint*) this) + 0);
-
- #if FW_OPENDOC_VERSION < FW_OPENDOC_DR4
- * (((ODPoint*) this) + 1) =
- #endif
- transform->InvertPoint(ev, ((ODPoint*) this) + 1);
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CRect::TransformCopy
- //----------------------------------------------------------------------------------------
-
- FW_CRect FW_CRect::TransformCopy(Environment* ev, ODTransform* transform) const
- {
- FW_CRect rect(*this);
-
- #if FW_OPENDOC_VERSION < FW_OPENDOC_DR4
- * (((ODPoint*) &rect) + 0) =
- #endif
- transform->TransformPoint(ev, ((ODPoint*) &rect) + 0);
-
- #if FW_OPENDOC_VERSION < FW_OPENDOC_DR4
- * (((ODPoint*) &rect) + 1) =
- #endif
- transform->TransformPoint(ev, ((ODPoint*) &rect) + 1);
-
- return rect;
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CRect::InverseTransformCopy
- //----------------------------------------------------------------------------------------
-
- FW_CRect FW_CRect::InverseTransformCopy(Environment* ev, ODTransform* transform) const
- {
- FW_CRect rect(*this);
-
- #if FW_OPENDOC_VERSION < FW_OPENDOC_DR4
- * (((ODPoint*) &rect) + 0) =
- #endif
- transform->InvertPoint(ev, ((ODPoint*) &rect) + 0);
-
- #if FW_OPENDOC_VERSION < FW_OPENDOC_DR4
- * (((ODPoint*) &rect) + 1) =
- #endif
- transform->InvertPoint(ev, ((ODPoint*) &rect) + 1);
-
- return rect;
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CRect::operator[]
- //----------------------------------------------------------------------------------------
-
- FW_CPoint& FW_CRect::operator[](FW_PointSelector sel)
- {
- return (sel == FW_kTopLeft) ? *((FW_CPoint *) &left) : *((FW_CPoint *) &right);
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CRect::operator[]
- //----------------------------------------------------------------------------------------
-
- const FW_CPoint& FW_CRect::operator[](FW_PointSelector sel) const
- {
- return (sel == FW_kTopLeft) ? *((FW_CPoint *) &left) : *((FW_CPoint *) &right);
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CRect::Size
- //----------------------------------------------------------------------------------------
-
- FW_CPoint FW_CRect::Size() const
- {
- return FW_CPoint(right - left, bottom - top);
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CRect::operator+=
- //----------------------------------------------------------------------------------------
-
- FW_CRect& FW_CRect::operator+=(const FW_CPoint& pt)
- {
- left += pt.x;
- top += pt.y;
- right += pt.x;
- bottom += pt.y;
-
- return *this;
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CRect::operator+
- //----------------------------------------------------------------------------------------
-
- FW_CRect FW_CRect::operator+(const FW_CPoint& pt) const
- {
- return FW_CRect(
- left + pt.x,
- top + pt.y,
- right + pt.x,
- bottom + pt.y);
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CRect::operator-=
- //----------------------------------------------------------------------------------------
-
- FW_CRect& FW_CRect::operator-=(const FW_CPoint& pt)
- {
- left -= pt.x;
- top -= pt.y;
- right -= pt.x;
- bottom -= pt.y;
-
- return *this;
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CRect::operator-
- //----------------------------------------------------------------------------------------
-
- FW_CRect FW_CRect::operator-(const FW_CPoint& pt) const
- {
- return FW_CRect(
- left - pt.x,
- top - pt.y,
- right - pt.x,
- bottom - pt.y);
- }
-
- //----------------------------------------------------------------------------------------
- // operator<<
- //----------------------------------------------------------------------------------------
-
- FW_FUNC_ATTR const FW_CWritableStream& operator<<(const FW_CWritableStream& stream, const FW_CRect& rect)
- {
- stream.Write(&rect, sizeof(FW_CRect));
- return stream;
- }
-
- //----------------------------------------------------------------------------------------
- // operator>>
- //----------------------------------------------------------------------------------------
-
- FW_FUNC_ATTR const FW_CReadableStream& operator>>(const FW_CReadableStream& stream, FW_CRect& rect)
- {
- stream.Read(&rect, sizeof(FW_CRect));
- return stream;
- }
-
-